﻿package org.xhs.report.common.ui.panel
{
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.toolbar.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.text.*;
    import flash.ui.*;
    import flash.utils.*;

    public class SearchPanel extends Sprite
    {
        protected var searchBar:Sprite;
        protected var searchBtn:Button;
        protected var searchKeywordTextfield:TextField;
        protected var searchResultPanel:Sprite;
        protected var searchResultNumTextField:TextField;
        protected var searchResultTotal:int = 0;
        protected var _searchResultIndex:int = 0;
        protected var searchPrev3Btn:Button;
        protected var searchNext3Btn:Button;
        protected var searchCloseBtn:Button;
        protected var _searchKeyWord:String;
        protected var _searchPageLoaded:Boolean = false;
        protected var _autoSearchIntervalID:int;
        protected var _autoSearchIndex:int = -1;
        protected var _autoSearchWaiting:Boolean = false;
        protected var screenModel:ScreenModel;
        protected var locatModel:ILocatModel;
        protected const TIPS:Object;

        public function SearchPanel(param1:ILocatModel)
        {
            this.TIPS = {search:"在文档内检索", searchp3:"在前3页检索", searchn3:"在后3页检索", searchp3_txt:"前一个结果", searchn3_txt:"后一个结果", searchclose:"取消检索"};
            this.locatModel = param1;
            this.initModel();
            this.init();
            return;
        }// end function

        protected function initModel() : void
        {
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            return;
        }// end function

        protected function screenResize(event:Event = null) : void
        {
            x = this.screenModel.stageWidth - 168 - Config.DV_WIDTH_RIGHT_SCROLLBAR;
            y = (this.screenModel.stageHeight - 1) - Config.SB_HEIGHT + (Config.SB_HEIGHT - Config.BTN_HEIGHT) / 2;
            if (this.screenModel.isFullscreen)
            {
                visible = false;
                this.searchCloseBtnClick();
            }
            else
            {
                visible = !Config.isAPIViewer;
            }
            return;
        }// end function

        protected function init() : void
        {
            var _loc_1:* = undefined;
            _loc_1 = new AllSkins();
            this.searchBar = new Sprite();
            var _loc_2:* = this.searchBar.graphics;
            _loc_2.clear();
            _loc_2.lineStyle(1, 12176325);
            _loc_2.beginFill(16777215);
            _loc_2.drawRect(0, 0, 135, 21);
            _loc_2.beginFill(16777215, 0);
            _loc_2.drawRect(135, 0, 21, 21);
            _loc_2.endFill();
            var _loc_3:* = new TextFormat("宋体", 14);
            _loc_3.align = "left";
            this.searchKeywordTextfield = new TextField();
            this.searchKeywordTextfield.type = "input";
            this.searchKeywordTextfield.defaultTextFormat = _loc_3;
            this.searchKeywordTextfield.width = 135;
            this.searchKeywordTextfield.height = 21;
            this.searchKeywordTextfield.htmlText = "<FONT COLOR=\'#999999\' SIZE=\'12\'>请输入检索关键词</FONT>";
            this.searchKeywordTextfield.x = 2;
            this.searchKeywordTextfield.y = 2;
            this.searchBar.addChild(this.searchKeywordTextfield);
            this.searchBtn = new Button();
            this.searchBtn.setStyle("skin", _loc_1["skin_searchbtn"]);
            this.searchBtn.x = 136;
            this.searchBtn.y = 0;
            this.searchBtn.useHandCursor = true;
            this.searchBar.addChildAt(this.searchBtn, 0);
            this.searchBtn.addEventListener(MouseEvent.CLICK, this.searchBtnClick);
            TooltipManager.getInstance().register(this.searchBtn, this.TIPS["search"]);
            this.searchBtn.enabled = false;
            this.searchResultPanel = new Sprite();
            var _loc_4:* = this.searchResultPanel.graphics;
            this.searchResultPanel.graphics.clear();
            _loc_4.lineStyle(0, 12176325);
            _loc_4.beginFill(15989753);
            _loc_4.drawRect(0, 0, 156, 42);
            _loc_4.drawRect(0, 42, 156, 20);
            this.searchResultNumTextField = new TextField();
            this.searchResultNumTextField.selectable = false;
            this.searchResultNumTextField.width = 150;
            this.searchResultNumTextField.multiline = true;
            this.searchResultNumTextField.wordWrap = true;
            this.searchResultNumTextField.htmlText = "";
            this.searchResultNumTextField.x = 5;
            this.searchResultNumTextField.y = 5;
            this.searchResultPanel.addChild(this.searchResultNumTextField);
            this.searchPrev3Btn = new Button();
            this.searchPrev3Btn.setStyle("skin", _loc_1[Config.isTxt ? ("skin_searchp3_txt") : ("skin_searchp3")]);
            this.searchPrev3Btn.x = 0;
            this.searchPrev3Btn.y = 41;
            this.searchPrev3Btn.useHandCursor = true;
            this.searchResultPanel.addChild(this.searchPrev3Btn);
            this.searchPrev3Btn.addEventListener(MouseEvent.CLICK, this.searchPrev3BtnClick);
            TooltipManager.getInstance().register(this.searchPrev3Btn, this.TIPS[Config.isTxt ? ("searchp3_txt") : ("searchp3")]);
            this.searchPrev3Btn.enabled = false;
            this.searchNext3Btn = new Button();
            this.searchNext3Btn.setStyle("skin", _loc_1[Config.isTxt ? ("skin_searchn3_txt") : ("skin_searchn3")]);
            this.searchNext3Btn.x = 67;
            this.searchNext3Btn.y = 41;
            this.searchNext3Btn.useHandCursor = true;
            this.searchResultPanel.addChild(this.searchNext3Btn);
            this.searchNext3Btn.addEventListener(MouseEvent.CLICK, this.searchNext3BtnClick);
            TooltipManager.getInstance().register(this.searchNext3Btn, this.TIPS[Config.isTxt ? ("searchn3_txt") : ("searchn3")]);
            this.searchNext3Btn.enabled = false;
            this.searchCloseBtn = new Button();
            this.searchCloseBtn.setStyle("skin", _loc_1["skin_searchclose"]);
            this.searchCloseBtn.x = 134;
            this.searchCloseBtn.y = 41;
            this.searchCloseBtn.useHandCursor = true;
            this.searchResultPanel.addChild(this.searchCloseBtn);
            this.searchCloseBtn.addEventListener(MouseEvent.CLICK, this.searchCloseBtnClick);
            TooltipManager.getInstance().register(this.searchCloseBtn, this.TIPS["searchclose"]);
            this.searchResultPanel.visible = false;
            addChild(this.searchBar);
            this.searchResultPanel.y = -63;
            addChildAt(this.searchResultPanel, 0);
            this.screenResize();
            try
            {
                ExternalInterface.addCallback(JSInterface.JS_SEARCH_HL, this.redByKey);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        protected function searchPrev3BtnClick(event:MouseEvent = null) : void
        {
            var _loc_3:int = 0;
            clearInterval(this._autoSearchIntervalID);
            var _loc_2:* = this.searchKeyWord;
            if (_loc_2 && _loc_2 != "")
            {
                _loc_3 = Math.max(this.locatModel.pageth - (Config.isTxt ? (1) : (3)), 0);
                this._searchPageLoaded = this.locatModel.isPagethLoaded(_loc_3);
                if (!Config.isTxt)
                {
                    this.locatModel.pageth = _loc_3;
                }
                if (this._searchPageLoaded)
                {
                    if (Config.isTxt)
                    {
                        var _loc_4:String = this;
                        var _loc_5:* = this.searchResultIndex - 1;
                        _loc_4.searchResultIndex = _loc_5;
                    }
                    dispatchEvent(new ToolbarEvent(ToolbarEvent.SEARCH_PREV));
                }
                else
                {
                    this._autoSearchIndex = _loc_3;
                    this._autoSearchWaiting = true;
                    this.searchResultPanel.visible = true;
                    this.searchResultNumTextField.text = (Config.isTxt ? ("当前") : ("前3")) + "页加载完成后将显示搜索结果，请稍候...";
                    this._autoSearchIntervalID = setInterval(this.autoSearch, 1000);
                }
                WenkuLog.addLog(WenkuLog.LOGURL_SEARCH + _loc_2);
            }
            return;
        }// end function

        protected function searchNext3BtnClick(event:MouseEvent = null) : void
        {
            var _loc_3:int = 0;
            clearInterval(this._autoSearchIntervalID);
            var _loc_2:* = this.searchKeyWord;
            if (_loc_2 && _loc_2 != "")
            {
                _loc_3 = Math.min(this.locatModel.pageth + (Config.isTxt ? (1) : (3)), (this.locatModel.pagesAllAvailable - 1));
                this._searchPageLoaded = this.locatModel.isPagethLoaded(_loc_3);
                if (!Config.isTxt)
                {
                    this.locatModel.pageth = _loc_3;
                }
                if (this._searchPageLoaded)
                {
                    if (Config.isTxt)
                    {
                        var _loc_4:String = this;
                        var _loc_5:* = this.searchResultIndex + 1;
                        _loc_4.searchResultIndex = _loc_5;
                    }
                    dispatchEvent(new ToolbarEvent(ToolbarEvent.SEARCH_NEXT));
                }
                else
                {
                    this._autoSearchIndex = _loc_3;
                    this._autoSearchWaiting = true;
                    this.searchResultPanel.visible = true;
                    this.searchResultNumTextField.text = (Config.isTxt ? ("当前") : ("后3")) + "页加载完成后将显示搜索结果，请稍候...";
                    this._autoSearchIntervalID = setInterval(this.autoSearch, 1000);
                }
                WenkuLog.addLog(WenkuLog.LOGURL_SEARCH + _loc_2);
            }
            return;
        }// end function

        protected function searchKeywordTextfieldFocusIn(event:FocusEvent = null) : void
        {
            if (this.searchKeywordTextfield.text == "请输入检索关键词")
            {
                this.searchKeywordTextfield.text = "";
            }
            this.showSearchResult();
            Config.isTextfieldActivated = true;
            return;
        }// end function

        protected function searchKeywordTextfieldFocusOut(event:FocusEvent = null) : void
        {
            if (this.searchKeywordTextfield.text == "")
            {
                this.searchKeywordTextfield.htmlText = "<FONT COLOR=\'#999999\' SIZE=\'12\' FACE=\'宋体\'>请输入检索关键词</FONT>";
            }
            Config.isTextfieldActivated = false;
            return;
        }// end function

        protected function redByKey(param1:String) : void
        {
            if (!param1)
            {
                return;
            }
            this.searchKeywordTextfield.text = param1;
            this.searchBtnClick();
            return;
        }// end function

        protected function autoSearch() : void
        {
            this._searchPageLoaded = this.locatModel.isPagethLoaded(this._autoSearchIndex);
            if (this._searchPageLoaded)
            {
                clearInterval(this._autoSearchIntervalID);
                this._autoSearchWaiting = false;
                dispatchEvent(new ToolbarEvent(ToolbarEvent.SEARCH_AUTO));
            }
            return;
        }// end function

        protected function searchKeywordTextfieldKeyUp(event:KeyboardEvent = null) : void
        {
            switch(event.keyCode)
            {
                case Keyboard.ENTER:
                case Keyboard.NUMPAD_ENTER:
                {
                    this.searchBtnClick();
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function searchBtnClick(event:MouseEvent = null) : void
        {
            clearInterval(this._autoSearchIntervalID);
            var _loc_2:* = this.searchKeyWord;
            if (_loc_2 && _loc_2 != "")
            {
                this._searchPageLoaded = this.locatModel.isPagethLoaded(this.locatModel.pageth);
                if (this._searchPageLoaded)
                {
                    dispatchEvent(new ToolbarEvent(ToolbarEvent.SEARCH_START));
                }
                else
                {
                    this._autoSearchIndex = this.locatModel.pageth;
                    this._autoSearchWaiting = true;
                    this.searchResultPanel.visible = true;
                    this.searchResultNumTextField.text = Config.isTxt ? ("当页加载完成后将显示搜索结果，请稍候...") : ("当前3页加载完成后将显示搜索结果，请稍候...");
                    this._autoSearchIntervalID = setInterval(this.autoSearch, 1000);
                }
                WenkuLog.addLog(WenkuLog.LOGURL_SEARCH + _loc_2);
            }
            return;
        }// end function

        public function showSearchResult(param1:int = -1) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            clearInterval(this._autoSearchIntervalID);
            var _loc_2:String = "";
            if (param1 == -1)
            {
                if (!this.searchKeyWord || this.searchKeyWord == "")
                {
                    _loc_2 = this.searchResultNumTextField.htmlText || "请输入检索关键词";
                }
                else
                {
                    _loc_2 = this.searchResultNumTextField.htmlText || "请输入关键词\n或点击按钮进行检索";
                }
            }
            else
            {
                param1 = param1 || 0;
                if (Config.isTxt)
                {
                    this.searchResultTotal = param1;
                    this.searchResultIndex = 0;
                }
                else
                {
                    this.searchPrev3Btn.enabled = this.locatModel.pageth != 0;
                    this.searchNext3Btn.enabled = this.locatModel.pageth < this.locatModel.pagesAllAvailable - 3 && this.locatModel.pagesAllAvailable > 3;
                }
                _loc_3 = Math.max(0, this.locatModel.pageth);
                _loc_4 = Math.min(this.locatModel.pageth + 2, (this.locatModel.pagesAllAvailable - 1));
                _loc_5 = Config.isTxt ? ("当前文本") : ("第 " + (_loc_3 + 1) + " - " + (_loc_4 + 1) + " 页");
                _loc_2 = "<font FACE=\"宋体\" SIZE=\"12\" COLOR=\"#666666\">" + _loc_5 + "内检索到<font face=\"Arial\" color=\"#f85106\"> <b>";
                _loc_2 = _loc_2 + (param1 + "</b> </font>条</font>");
            }
            this.searchResultPanel.visible = true;
            this.searchResultNumTextField.htmlText = _loc_2;
            return;
        }// end function

        public function searchCloseBtnClick(event:MouseEvent = null) : void
        {
            clearInterval(this._autoSearchIntervalID);
            this._searchPageLoaded = false;
            this._autoSearchIndex = -1;
            this._autoSearchWaiting = false;
            this.searchPrev3Btn.enabled = false;
            this.searchNext3Btn.enabled = false;
            dispatchEvent(new ToolbarEvent(ToolbarEvent.SEARCH_CLOSE));
            this.searchResultNumTextField.htmlText = "";
            this.searchResultPanel.visible = false;
            return;
        }// end function

        public function set searchResultIndex(param1:int) : void
        {
            this._searchResultIndex = param1;
            if (this._searchResultIndex == 0)
            {
                this.searchPrev3Btn.enabled = false;
            }
            else
            {
                this.searchPrev3Btn.enabled = true;
            }
            if (this._searchResultIndex == (this.searchResultTotal - 1) || !this.searchResultTotal)
            {
                this.searchNext3Btn.enabled = false;
            }
            else
            {
                this.searchNext3Btn.enabled = true;
            }
            return;
        }// end function

        public function get searchResultIndex() : int
        {
            return this._searchResultIndex;
        }// end function

        public function set enabled(param1:Boolean) : void
        {
            if (param1)
            {
                this.searchBtn.enabled = true;
                this.searchKeywordTextfield.type = TextFieldType.INPUT;
                this.searchKeywordTextfield.addEventListener(FocusEvent.FOCUS_IN, this.searchKeywordTextfieldFocusIn);
                this.searchKeywordTextfield.addEventListener(FocusEvent.FOCUS_OUT, this.searchKeywordTextfieldFocusOut);
                this.searchKeywordTextfield.addEventListener(KeyboardEvent.KEY_UP, this.searchKeywordTextfieldKeyUp);
            }
            else
            {
                this.searchBtn.enabled = false;
                this.searchKeywordTextfield.type = TextFieldType.DYNAMIC;
                this.searchKeywordTextfield.removeEventListener(FocusEvent.FOCUS_IN, this.searchKeywordTextfieldFocusIn);
                this.searchKeywordTextfield.removeEventListener(FocusEvent.FOCUS_OUT, this.searchKeywordTextfieldFocusOut);
                this.searchKeywordTextfield.removeEventListener(KeyboardEvent.KEY_UP, this.searchKeywordTextfieldKeyUp);
            }
            return;
        }// end function

        public function get searchKeyWord() : String
        {
            return this.searchKeywordTextfield.text;
        }// end function

        public function get autoSearchIndex() : int
        {
            return this._autoSearchIndex;
        }// end function

        public function get isOpen() : Boolean
        {
            return this.searchResultPanel.visible;
        }// end function

    }
}
